package com.mrlee.controller;

import com.mrlee.bean.Admin;
import com.mrlee.bean.Module;
import com.mrlee.bean.Role;
import com.mrlee.service.AdminService;
import com.mrlee.service.ModuleService;
import com.mrlee.service.RoleService;
import com.mrlee.utils.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/9.
 */
@Controller
public class MainController {
    @Resource
    private ModuleService moduleService;
    @Resource
    private RoleService roleServce;

    @Resource
    private AdminService adminService;

    // 登录页面的地址导航
    @RequestMapping(value = "/login")
    public String loginpage(){

        if (SecurityUtils.getSubject().isAuthenticated()){
            return "redirect:home";
        }
        return "login";
    }

    @RequestMapping(value = "/index")
    public String index(){
        return "index";
    }


    @RequestMapping(value = "/loginsubmit")
    public String loginsubmit(HttpServletRequest request) throws Exception {

        // 因为表单过滤器的原因, 这个方法不需要接受用户名和密码, 在进行login
        // 而是由过滤器直接吧用户名密码交给realm来做认证

        // 我们在这个方法中, 就只处理异常就可以了

        // 过滤器先于Servlet执行, 所以当在realm中处理认证时出现异常
        // 过滤器会把异常信息保存到request中发给Servlet, 即交给我们来处理

        // 1. 获取异常的名字

        String exName = (String) request.getAttribute("shiroLoginFailure");

        if (UnknownAccountException.class.getName().equals(exName)){
            throw new UnknownAccountException("账号不存在");
        } else if (IncorrectCredentialsException.class.getName().equals(exName)){
            throw new IncorrectCredentialsException("密码错误");
        } else {
            throw new Exception("我也不知道为啥不对");
        }
    }

    //    @RequiresRoles(value = "CTO")
    @RequestMapping(value = "/test")
    public String test(){

        if (SecurityUtils.getSubject().hasRole("CTO")){
            return "test";
        } else {
            return "redirect:home";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/selectAllModules")
    public List<String> selectAllModules(){
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin) subject.getPrincipal();
        String name = admin.getName();

        List<Module> moduleList = moduleService.selectModule(name);
        List<String> list = new ArrayList<>();
        for (Module moduleInfo : moduleList){
            list.add(moduleInfo.getName());
        }
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "selectuser")
    public AjaxResult selectUser(){
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin) subject.getPrincipal();
        List<Role> roleList = roleServce.selectRoleList(admin.getAdminId());
        admin.setRoleList(roleList);
        System.out.println(admin);
        AjaxResult result = new AjaxResult();
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(admin);
        return result;
    }
}
